home *** CD-ROM | disk | FTP | other *** search
/ Network Supervisor's Toolkit / Network Supervisor's Toolkit.iso / tools / nwtp06 / m_pep.pas < prev    next >
Pascal/Delphi Source File  |  1996-07-10  |  5KB  |  176 lines

  1. {$X+,V-,B-}
  2. program M_PEP; { Master / Sender }
  3.  
  4. { Testprogram for the nwPEP unit / NwTP 0.6 API. (c) 1993,1995, R.Spronk }
  5.  
  6. { Sents a single packet, waits for acknowledgement }
  7.  
  8. uses crt,nwMisc,nwBindry,nwConn,nwIPX,nwPEP;
  9.  
  10. CONST IOSocket=$5678;           { socket to transmit/receive on }
  11.  
  12. Var ListenECB     :Tecb;        { ECB and header, to listen for acknowledgement }
  13.     ListenPepHdr  :TpepHeader;
  14.  
  15.     SendECB       :Tecb;        { ECB and header, used to send the data }
  16.     SendPepHdr    :TpepHeader;
  17.  
  18.     socket        :word;
  19.  
  20.     buf           :array[1..546] of byte; { buffer for data to be sent }
  21.  
  22.     AckReceived   :boolean;     { set to true within the ListenForAckESR }
  23.  
  24.     SendTransId   :LongInt;     { transactionID. This uniquely identifies
  25.                                   the packet. The slave/receiver has to
  26.                                   reply with the same transactionID in the
  27.                                   header of the acknowledgement. Only if
  28.                                   this number is the same as the transactioID
  29.                                   of the sent packet, the pavket is considered
  30.                                   successfully delivered. }
  31.  
  32.     NewStack:array[1..1024] of word;  { !! used by ESR }
  33.     StackBottom:word;                 { !! used by ESR }
  34.  
  35.  
  36. {$F+}
  37. Procedure ListenForAckHandler(Var p:TPecb);
  38.  { Interrupts are turned off -and should remain turned off- }
  39. begin
  40. IF (ListenECB.CompletionCode<>0)                      { packet must be suucessfully received.. }
  41.  or (ListenPepHdr.IPXhdr.packetType<>PEP_PACKET_TYPE) { of type PEP.. }
  42.  or (ListenPepHdr.ClientType<>$EA)                    { of client type $EA }
  43.  or (ListenPepHdr.TransactionID<>SendTransId)         { with a correct clientID (of the packet the master sent) }
  44.   then IPXListenForPacket(ListenECB)   { Invalid packet => listen again   }
  45.   else AckReceived:=true;              { valid packet   => ACK received ! }
  46. end;
  47. {$F-}
  48.  
  49. {$F+}
  50. Procedure ListenForAckESR; assembler;
  51. asm { ES:SI are the only valid registers when entering this procedure ! }
  52.     { interrupts are turned off -and should remain turned off- }
  53.     mov dx, seg stackbottom
  54.     mov ds, dx
  55.  
  56.     mov dx,ss  { setup of a new local stack }
  57.     mov bx,sp  { ss:sp copied to dx:bx}
  58.     mov ax,ds
  59.     mov ss,ax
  60.     mov sp,offset stackbottom
  61.     push dx    { push old ss:sp on new stack }
  62.     push bx
  63.  
  64.     push es    { push es:si on stack as local vars }
  65.     push si
  66.     mov  di,sp
  67.  
  68.     push ss    { push address of local ptr on stack }
  69.     push di
  70.     CALL ListenForAckHandler
  71.  
  72.     add sp,4   { skip stack ptr-copy }
  73.     pop bx     { restore ss:sp from new stack }
  74.     pop dx
  75.     mov sp,bx
  76.     mov ss,dx
  77. end;
  78. {$F-}
  79.  
  80.  
  81. Var dest:TinternetworkAddress;
  82.     ticks,ticks2:word;
  83.     retries     :word;
  84.  
  85.     Uname:string;
  86.     NbrOfConn:byte;
  87.     connList:TconnectionList;
  88.  
  89. begin
  90. If paramcount<>1
  91.  then begin
  92.       writeln('Usage: M_PEP <username>');
  93.       writeln('-a test pep packet will be sent to the workstation of this user.');
  94.       writeln('-run S_PEP on that workstation to receive the packet.');
  95.       halt(1);
  96.       end;
  97. Uname:=ParamStr(1);
  98. UpString(Uname);
  99. NbrOfConn:=0;
  100. IF (NOT GetObjectConnectionNumbers(Uname,OT_USER,NbrOfConn,connList))
  101.    or (NbrOfConn=0)
  102.  then begin
  103.       writeln('Error: can''t locate user ',Uname);
  104.       halt(1);
  105.       end;
  106. IF NbrOfConn>1
  107.  then begin
  108.       writeln('The specified user has multiple connections.');
  109.       writeln('This demonstation program doesn''t support multiple connections.');
  110.       halt(1);
  111.       end;
  112. IF NOT GetInternetAddress(connList[1],dest)
  113.  then begin
  114.       writeln('Error: can''t find the address of user ',Uname);
  115.       halt(1);
  116.       end;
  117.  
  118.  
  119. IF NOT IpxInitialize
  120.  then begin
  121.       writeln('Ipx needs to be installed.');
  122.       halt(1);
  123.       end;
  124. socket:=IOSocket;
  125. IF NOT IPXopenSocket(Socket,SHORT_LIVED_SOCKET)
  126.  then begin
  127.       writeln('IPXopenSocket returned error# ',nwIPX.result);
  128.       halt(1);
  129.       end;
  130.  
  131.  
  132. { setup listening for ack }
  133. AckReceived:=False;
  134. FillChar(buf,546,#0);
  135. { Setup ECB and IPX header }
  136. PEPsetupListenECB(Addr(ListenForAckESR),IOsocket,@buf,546,
  137.                   ListenPepHdr,ListenECB);
  138. IPXListenForPacket(ListenECB);
  139.  
  140. { send packet }
  141.  
  142. dest.socket:=IOsocket;
  143. buf[1]:=ord('s');buf[2]:=ord('m');
  144. PEPsetupSendECB(NIL,IOsocket,dest,@buf[1],2,
  145.                 SendPepHdr,SendECB);
  146. SendTransID:=1;
  147. SendPepHdr.TransactionId:=SendTransId;
  148. SendPepHdr.ClientType:=$EA;
  149. IPXsendPacket(SendECB);
  150. writeln('Packet was sent.');
  151. while sendECB.InuseFlag<>0 do IPXrelinquishControl;
  152. IPXGetIntervalMarker(ticks);
  153.  
  154. { wait for acknowledgement or timeout }
  155. retries:=0;
  156.  
  157. REPEAT
  158.   IPXrelinquishcontrol;
  159.   IPXGetIntervalMarker(ticks2);
  160.   if (ticks2-ticks)>4
  161.    then begin
  162.         inc(retries);
  163.         writeln('Timeout: resending');
  164.         IPXsendPacket(SendECB);
  165.         while sendECB.InuseFlag<>0 do IPXrelinquishControl;
  166.         IPXGetIntervalMarker(ticks);
  167.         end;
  168. UNTIL AckReceived or Keypressed or (retries>50);
  169.  
  170. if AckReceived
  171.  then writeln('Ack was received.');
  172.  
  173. IF NOT IPXcloseSocket(IOsocket)
  174.  then writeln('IPXcloseSocket returned error# ',nwIPX.result);
  175.  
  176. end.